package com.atguigu.examsystemserver.controller;

import com.atguigu.examsystemserver.common.Result;
import com.atguigu.examsystemserver.entity.Banner;
import com.atguigu.examsystemserver.service.BannerService;
import com.atguigu.examsystemserver.service.FileUploadService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


import java.util.List;

@RestController
@RequestMapping("/api/banners")
public class BannerController {
    @Autowired
    BannerService bannerService;
    @Autowired
    FileUploadService fileUploadService;

    @Operation(summary = "获取所有轮播图")
    @GetMapping("/list")
    public Result<List<Banner>> allList() {
        List<Banner> bannerList = bannerService.list();
        return Result.success(bannerList, "获取所有轮播图成功");
    }

    @Operation(summary = "根据ID获取轮播图")
    @GetMapping("/{id}")
    public Result<Banner> getById(@PathVariable Integer id){
        Banner banner = bannerService.getById(id);
        return Result.success(banner,"根据ID获取轮播图成功");
    }

    @Operation(summary = "获取启用的轮播图")
    @GetMapping("/active")
    public Result<List<Banner>> getActiveList(){
        LambdaQueryWrapper<Banner> bannerLambdaQueryWrapper = new LambdaQueryWrapper<>();
        bannerLambdaQueryWrapper.eq(Banner::getIsActive,true);
        List<Banner> list = bannerService.list(bannerLambdaQueryWrapper);
        return Result.success(list,"获取启用的轮播图成功");
    }

    @Operation(summary = "添加轮播图")
    @PostMapping("/add")
    public Result save(@RequestBody Banner banner){
        boolean save = bannerService.save(banner);
        if (save){
            return Result.success("添加轮播图成功");
        }else {
            return Result.error("添加轮播图失败");
        }
    }

    @Operation(summary = "删除轮播图")
    @DeleteMapping("/delete/{id}")
    public Result removeById(@PathVariable Integer id){
        boolean b = bannerService.removeById(id);
        if (b){
            return Result.success("删除轮播图成功");
        }else {
            return Result.error("删除轮播图失败");
        }
    }

    @Operation(summary = "更新轮播图")
    @PutMapping("/update")
    public Result update(@RequestBody Banner banner){
        boolean b = bannerService.updateById(banner);
        if (b){
            return Result.success("更新轮播图成功");
        }else {
            return Result.error("更新轮播图失败");
        }
    }

    @Operation(summary = "切换轮播图状态")
    @PutMapping("/toggle/{id}")
    public Result updateActive(@RequestParam Boolean isActive,@PathVariable Integer id){
        LambdaUpdateWrapper<Banner> bannerLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        bannerLambdaUpdateWrapper.set(Banner::getIsActive,isActive).eq(Banner::getId,id);
        boolean update = bannerService.update(bannerLambdaUpdateWrapper);
        if (update){
            return Result.success("切换轮播图状态成功");
        }else {
            return Result.error("切换轮播图状态失败");
        }
    }

    @Operation(summary = "上传轮播图")
    @PostMapping("/upload-image")
    public Result upload(MultipartFile file){
        String uploadfile = fileUploadService.uploadfile(file);
        return Result.success(uploadfile,"上传轮播图成功");
    }
}
